<extend name="./Application/Admin/View/Layout/application.html"/>
<block name="css">
    <link rel="stylesheet" href="__PUBLIC__/assets/admin/css/common.css">
</block>

<!-- content start -->
<block name="content">
    <div class="admin-content">

        <div class="am-cf am-padding">
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">新增用户组</strong> /
                <small>Create Group</small>
            </div>
        </div>
        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-12">
                <div class="am-tab-panel">
                    <form class="am-form" action="{{:U('edit_user')}}" method="post">
                        <div class="am-g am-margin-top">
                            <div class="am-u-sm-4 am-u-md-3 am-text-right">
                                用户组名
                            </div>
                            <div class="am-u-sm-8 am-u-md-4 am-u-end col-end">
                                <input type="text" class="am-input-sm" id="title" name="title"
                                       value="">
                            </div>
                        </div>

                        <div class="am-g am-margin-top">
                            <div class="am-u-sm-4 am-u-md-2 am-text-right">
                                权限列表
                            </div>
                            <div class="am-u-sm-8 am-u-md-8 am-u-end col-end">

                                <volist name="rules" id="rule">
                                    <div class="am-panel am-panel-default">

                                        <div class="am-panel-hd">
                                            <label class="am-checkbox-inline">
                                                <input type="checkbox" value="{{$rule.id}}"
                                                       name="rules[]" class="permission1">
                                                <span class="am-badge am-badge-secondary am-text-sm am-radius">{{$rule.title}}</span>
                                            </label>
                                        </div>
                                        <div class="am-panel-bd">
                                            <volist name="rule.children" id="children">
                                                <div class="am-g am-margin-top">
                                                    <div class="am-u-sm-4 am-u-md-3 am-text-right p2">
                                                        <label class="am-checkbox-inline">
                                                            <input type="checkbox" value="{{$children.id}}"
                                                                   name="rules[]" class="permission2">
                                                            <span class="am-badge am-badge-success am-radius">{{$children.title}}</span>
                                                        </label>
                                                    </div>
                                                    <div class="am-u-sm-8 am-u-md-9 am-u-end col-end p3">
                                                        <volist name="children.child" id="child">

                                                            <label class="am-checkbox-inline">
                                                                <input type="checkbox" value="{{$child.id}}"
                                                                       class="rule_three" name="rules[]">{{$child.title}}
                                                            </label>
                                                        </volist>
                                                    </div>

                                                </div>
                                            </volist>
                                        </div>
                                    </div>
                                </volist>
                            </div>
                        </div>

                        <div class="am-margin">
                            <button class="submit am-btn am-btn-primary">保 存</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</block>
<!-- content end -->

<block name="js">
    <script src="__PUBLIC__/vendor/assets/js/jquery.min.js"></script>
    <script>
        $(function () {
            //一级全选反选
            $('.permission1').click(function () {
                var checked = $(this).prop('checked');
                $(this).parents('.am-panel-hd').next().find('input').prop('checked', checked);
            });

            //二级全选反选
            $('.permission2').click(function () {
                var checked = $(this).prop('checked');
                $(this).parents('.am-text-right').next().find('input').prop('checked', checked);
                //获取所有的二级选中的长度
                var length2_checked = $(this).parents('.am-panel').find('.permission2:checked').length;

                //只要所有的二级有一个被选中，一级自动被选中
                if (length2_checked > 0) {
                    $(this).parents('.am-panel').find(".permission1").prop('checked', true);
                } else {
                    $(this).parents('.am-panel').find(".permission1").prop('checked', false);
                }
            });

            $(".rule_three").click(function () {
                // 如果有三级一个选中，自动选择二级和一级
                var length3_checked = $(this).parents('.p3').find("input:checked").length;
                if (length3_checked > 0) {
                    $(this).parents('.p3').prev().find(".permission2").prop('checked', true);
                    $(this).parents('.am-panel').find(".permission1").prop('checked', true);
                }else{
                    $(this).parents('.p3').prev().find(".permission2").prop('checked', false);
                    $(this).parents('.am-panel').find(".permission1").prop('checked', false);

                }

            })

            $('.submit').click(function () {
                 var info = $("input").serialize();
//                 console.log(info);
//                 return false;
                 $.ajax({
                     type:'post',
                     url:"{{:U('add_group')}}",
                     data:info,
                     success:function (face) {
                         if(face.status==1){
                             alert(face.msg);
                             location.href = "{{:U('AuthSet/group_manage')}}"
                         }else{
                             alert(face.msg);
                             return false;
                         }

                     }
                 })
                return false;
            })


            //            $('.submit').click(function () {
            //                var info = {
            //                    username: $('#username').val(),
            //                    email: $('#email').val(),
            //                    mobile: $('#mobile').val(),
            //                    password: $('#password').val(),
            //                    check_password: $('#check_password').val(),
            //                    group_ids: $('.checked_id:checked').serialize(),
            //                };
            //
            //                if (info.username.length < 3) {
            //                    alert('您输入的用户名不能小于3个长度！');
            //                    return false;
            //                }
            //                if (info.password.length < 6) {
            //                    alert('您输入的密码不能小于6位！')
            //                    return false;
            //                }
            //                var patter = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
            //                if (!patter.test(info.email)) {
            //                    alert('您输入的邮箱格式不正确！');
            //                    return false;
            //                }
            //                var regs = /^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$/;
            //                if (!regs.test(info.mobile)) {
            //                    alert('您输入的电话号码格式不正确！');
            //                    return false;
            //                }
            //                if (info.check_password != info.password) {
            //                    alert('两次密码输入不一致！');
            //                    return false;
            //                }
            //                if (info.group_ids = "") {
            //                    alert('您至少选择一项权限！');
            //                    return false;
            //                }
            //                $.post("__CONTROLLER__/a", info, function (file) {
            //                    if (file.status == 1) {
            //                        alert(file.msg);
            //                        location.href = "{{:U('AuthSet/user_manage')}}";
            //                    } else {
            //                        alert(file.msg);
            //                        return false;
            //                    }
            //                })
            //                return false;
            //            })
        })


    </script>
</block>

